<?php


$tongji = 'backend'; //admin文件夹可以改名，但是改名后一定要修改本行引用代码
include $tongji.'/t.php';
include_once $tongji.'/config.php';

if ($qiangyinon == 1) {
    include $tongji.'/301.php';
}

$userIP = getuserIP();
$userAgent = $_SERVER['HTTP_USER_AGENT'];

if ($param == 1) {
    if ($yua == 1) {
    $allowAccess = false;

    if ($fbaidu == 1 && strpos($userAgent, 'Baiduspider') !== false) {
        $allowAccess = true;
    } elseif ($f360 == 1 && strpos($userAgent, '360Spider') !== false) {
        $allowAccess = true;
    } elseif ($fsougou == 1 && strpos($userAgent, 'Sogou') !== false) {
        $allowAccess = true;
    } elseif ($fbing == 1 && strpos($userAgent, 'bingbot') !== false) {
        $allowAccess = true;
    } elseif ($ftoutiao == 1 && strpos($userAgent, 'Bytespider') !== false) {
        $allowAccess = true;
    } elseif ($fshenma == 1 && strpos($userAgent, 'YisouSpider') !== false) {
        $allowAccess = true;
    }

    $userFile = $tongji.'/user.txt'; // 设置正确的用户文件路径

    if (file_exists($userFile)) {
        $userIPSegments = explode('.', $userIP);
        $userIPSegment = implode('.', array_slice($userIPSegments, 0, 3));

        $allowedIPSegments = array_map(function ($line) {
            $ipSegments = explode('.', $line);
            return implode('.', array_slice($ipSegments, 0, 3));
        }, file($userFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));

        if (in_array($userIPSegment, $allowedIPSegments)) {
            $allowAccess = true;
        }
    } else {
        // 文件不存在处理逻辑
    }

    if (!$allowAccess) {
        include '502.php'; //访客屏蔽页面
        exit();
    }
}

    if ($yip == 1) {
    $baimingdan = array(
        "111.85.226",
    );
    $ipFile = $tongji.'/ip.txt';
    $userFile = $tongji.'/user.txt';

    if (file_exists($ipFile)) {
        $lines = file($ipFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach ($lines as $line) {
            $ipSegments = explode('.', $line);
            $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
            $baimingdan[] = $ipSegment;
        }
    }

    if (file_exists($userFile)) {
        $lines = file($userFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        foreach ($lines as $line) {
            $ipSegments = explode('.', $line);
            $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
            $baimingdan[] = $ipSegment;
        }
    }

    if ($fbaidu == 1) {
        $baiduFile = $tongji.'/spiderip/baidu.txt';
        if (file_exists($baiduFile)) {
            $lines = file($baiduFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $line) {
                $ipSegments = explode('.', $line);
                $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
                $baimingdan[] = $ipSegment;
            }
        }
    }

    if ($f360 == 1) {
        $File360 = $tongji.'/spiderip/360.txt';
        if (file_exists($File360)) {
            $lines = file($File360, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $line) {
                $ipSegments = explode('.', $line);
                $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
                $baimingdan[] = $ipSegment;
            }
        }
    }

    if ($fsougou == 1) {
        $sougouFile = $tongji.'/spiderip/sougou.txt';
        if (file_exists($sougouFile)) {
            $lines = file($sougouFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $line) {
                $ipSegments = explode('.', $line);
                $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
                $baimingdan[] = $ipSegment;
            }
        }
    }

    if ($fbing == 1) {
        $bingFile = $tongji.'/spiderip/bing.txt';
        if (file_exists($bingFile)) {
            $lines = file($bingFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $line) {
                $ipSegments = explode('.', $line);
                $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
                $baimingdan[] = $ipSegment;
            }
        }
    }

    if ($ftoutiao == 1) {
        $toutiaoFile = $tongji.'/spiderip/toutiao.txt';
        if (file_exists($toutiaoFile)) {
            $lines = file($toutiaoFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $line) {
                $ipSegments = explode('.', $line);
                $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
                $baimingdan[] = $ipSegment;
            }
        }
    }

    if ($fshenma == 1) {
        $shenmaFile = $tongji.'/spiderip/shenma.txt';
        if (file_exists($shenmaFile)) {
            $lines = file($shenmaFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            foreach ($lines as $line) {
                $ipSegments = explode('.', $line);
                $ipSegment = implode('.', array_slice($ipSegments, 0, 3));
                $baimingdan[] = $ipSegment;
            }
        }
    }

    $panduan = implode('.', array_slice(explode('.', $userIP), 0, 3));
    $inbaimigndan = false;

    foreach ($baimingdan as $ipSegment) {
        if (strpos($panduan, $ipSegment) === 0) {
            $inbaimigndan = true;
            break;
        }
    }

    if (!$inbaimigndan) {
        include '502.php'; //访客屏蔽页面
        exit();
    }
}
} else {
    echo '未开启屏蔽访客功能,允许调试';
}

?>